package top.mindse.office2pdf.util;

import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * PDF 工具
 */
public class PDFUtils {
    private static final Logger logger = LoggerFactory.getLogger(PDFUtils.class);

    /**
     * 添加水印
     *
     * @param in
     * @param out
     * @param watermark
     * @throws IOException
     */
    public static void addWatermark(InputStream in, OutputStream out,
                                    String watermark) throws IOException {
        try {
            PdfReader reader = new PdfReader(in);
            PdfStamper stamper = new PdfStamper(reader, out);
            BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED, true);
            Rectangle pageSize = null;
            PdfGState gs = new PdfGState();
            gs.setFillOpacity(0.1f);
            gs.setStrokeOpacity(0.4f);
            int pageCount = reader.getNumberOfPages() + 1;
            Graphics2D g = (Graphics2D) new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB).getGraphics();
            int textWidth = (int) new Font("宋体", Font.PLAIN, 24).getStringBounds(watermark, g.getFontRenderContext()).getWidth();
            PdfContentByte pcb;
            for (int i = 1; i < pageCount; i++) {
                pageSize = reader.getPageSizeWithRotation(i);
                int wn = (int) Math.ceil(pageSize.getWidth() / textWidth);
                int hn = (int) Math.ceil(pageSize.getHeight() / textWidth);
                pcb = stamper.getOverContent(i);
                pcb.saveState();
                pcb.setGState(gs);
                pcb.beginText();
                pcb.setFontAndSize(font, 24);
                for (int m = 0; m < hn; m++) {
                    for (int n = 0; n < wn; n++) {
                        pcb.showTextAligned(Element.ALIGN_LEFT, watermark, 24 + n * textWidth, m * textWidth, 45);
                    }
                }
                pcb.endText();
            }
            stamper.close();
            reader.close();
        } catch (DocumentException e) {
            logger.error("PDF添加水印异常", e);
        }
    }
}
